package com.prp.db.daoimpl.mysql;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.db.idao.ISubjectCommentDAO;
import com.prp.db.po.SubjectComment;

/**
 * A data access object (DAO) providing persistence and search support for
 * SubjectComment entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .SubjectComment
 * @author MyEclipse Persistence Tools
 */

public class SubjectCommentDAO extends BaseHibernateDAO implements
		ISubjectCommentDAO {
	private static final Log log = LogFactory.getLog(SubjectCommentDAO.class);
	// property constants
	public static final String TO_SUBJECT_ID = "toSubjectId";
	public static final String COMMENT_USER_ID = "commentUserId";
	public static final String CONTENT = "content";
	public static final String SCORE = "score";

	public void save(SubjectComment transientInstance) {
		log.debug("saving SubjectComment instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public Double getAverageScore(int subjectId) {
		try {
			String queryString = "select avg(" + SCORE
					+ ") from SubjectComment as model where model."
					+ TO_SUBJECT_ID + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, subjectId);
			return (Double)queryObject.list().get(0);
		} catch (RuntimeException re) {
			log.error("get average failed", re);
			throw re;
		}
	}
	
	public void delete(SubjectComment persistentInstance) {
		log.debug("deleting SubjectComment instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SubjectComment findById(java.lang.Integer id) {
		log.debug("getting SubjectComment instance with id: " + id);
		try {
			SubjectComment instance = (SubjectComment) getSession().get(
					"SubjectComment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SubjectComment instance) {
		log.debug("finding SubjectComment instance by example");
		try {
			List results = getSession().createCriteria("SubjectComment").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SubjectComment instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SubjectComment as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByToSubjectId(Object toSubjectId) {
		return findByProperty(TO_SUBJECT_ID, toSubjectId);
	}

	public List findByCommentUserId(Object commentUserId) {
		return findByProperty(COMMENT_USER_ID, commentUserId);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByScore(Object score) {
		return findByProperty(SCORE, score);
	}

	public List findAll() {
		log.debug("finding all SubjectComment instances");
		try {
			String queryString = "from SubjectComment";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SubjectComment merge(SubjectComment detachedInstance) {
		log.debug("merging SubjectComment instance");
		try {
			SubjectComment result = (SubjectComment) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SubjectComment instance) {
		log.debug("attaching dirty SubjectComment instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SubjectComment instance) {
		log.debug("attaching clean SubjectComment instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}